﻿using Rex.Assignment.Application.AppUsers.Dtos;
using Rex.Assignment.Common;
using Rex.Assignment.Domain.Models.Entitys;
using Rex.Assignment.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Rex.Assignment.Application.AppUsers.Impl
{
    /// <summary>
    /// 系统用户
    /// </summary>
    /// <remarks>
    /// @创 建 者：Rex
    /// @创建日期：2021/2/21 16:17:37
    /// </remarks>
    public class UserAppService : IUserAppService
    {
        private readonly ISysUserRepository _sysUserRepository;

        public UserAppService(ISysUserRepository sysUserRepository)
        {
            _sysUserRepository = sysUserRepository;
        }

        /// <summary>
        /// 获取系统用户信息
        /// </summary>
        /// <typeparam name="type">排序的字段类型</typeparam>
        /// <param name="isAsc">是否升序</param>
        /// <param name="orderByExpression">排序表达式</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns></returns>
        public DataResult<IList<SysUserDto>> GetSysUserList<type>(bool isAsc, Expression<Func<SysUser, type>> orderByExpression, Expression<Func<SysUser, bool>> whereExpression)
        {
            DataResult<IList<SysUserDto>> dataResult = new DataResult<IList<SysUserDto>>();
            try
            {
                // a.查询数据
                IList<SysUser> sysUserList = _sysUserRepository.GetModels<type>(isAsc, orderByExpression, whereExpression).ToList();
                // b.转换为Dto
                IList<SysUserDto> userDtoList = sysUserList.MapToList<SysUser, SysUserDto>();
                dataResult.Data = userDtoList;
                dataResult.Msg = "获取系统用户信息成功！";
            }
            catch (Exception ex)
            {
                dataResult.Msg = ex.Message;
                dataResult.Code = StatusCode.Error;
                throw;
            }
            return dataResult;
        }
    }
}
